Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
snapdragon
Advanced tools
The snapdragon npm package is a highly pluggable, low-level compiler for creating compilers and other text processing applications. It provides a robust framework for parsing, compiling, and rendering custom grammars in a structured way.
Parsing
This feature allows you to define custom parsing rules. The code sample demonstrates how to create a simple text parser that recognizes sequences of lowercase letters.
const Snapdragon = require('snapdragon');
const snapdragon = new Snapdragon();
snapdragon.parser.use(function() {
this.set('text', function() {
const pos = this.position();
const m = this.match(/^[a-z]+/);
if (!m) return;
return pos(this.node({type: 'text', val: m[0]}));
});
});
const ast = snapdragon.parse('hello');
console.log(ast);
Compiling
This feature allows you to compile parsed input into a new form. The code sample shows how to compile a parsed abstract syntax tree (AST) into uppercase text.
const Snapdragon = require('snapdragon');
const snapdragon = new Snapdragon();
snapdragon.compiler.use(function(node) {
if (node.type === 'text') {
return node.val.toUpperCase();
}
});
const ast = snapdragon.parse('hello');
const result = snapdragon.compile(ast).output;
console.log(result);
Rendering
This feature involves rendering the compiled output. The code sample illustrates how to render the output of a parsed AST, adding a space after each text node.
const Snapdragon = require('snapdragon');
const snapdragon = new Snapdragon();
snapdragon.renderer.use(function(node) {
if (node.type === 'text') {
this.emit(node.val + ' '); // add space after each text node
}
});
const ast = snapdragon.parse('hello world');
const result = snapdragon.render(ast);
console.log(result);
Chevrotain is a JavaScript parser building toolkit which is similar to snapdragon but focuses more on performance and ease of use for creating complex parsers without writing too much boilerplate code. It differs from snapdragon in that it provides a richer set of built-in features for defining grammars and performing lexical analysis.
Nearley is another parser toolkit that uses a different approach by leveraging grammar files written in a special format. It is similar to snapdragon in that it allows for the creation of custom parsers, but it automates more of the process and can handle more complex, ambiguous grammars.
PEG.js is a simple parser generator that allows you to create parsers directly from a string representation of a Parsing Expression Grammar. It is similar to snapdragon in its purpose but differs in that it generates parsers from a high-level description rather than requiring manual definition of parsing behavior.
Fast, pluggable and easy-to-use parser-renderer factory.
Install with npm:
$ npm install --save snapdragon
Created by jonschlinkert and doowb.
Features
Breaking changes
Substantial breaking changes were made in v0.5.0! Most of these changes are part of a larger refactor that will be finished in 0.6.0, including the introduction of a Lexer
class.
Compiler
.render
method was renamed to .compile
var Snapdragon = require('snapdragon');
var snapdragon = new Snapdragon();
Parse
var ast = snapdragon.parser('some string', options)
// parser middleware that can be called by other middleware
.set('foo', function () {})
// parser middleware, runs immediately in the order defined
.use(bar())
.use(baz())
Render
// pass the `ast` from the parse method
var res = snapdragon.compiler(ast)
// compiler middleware, called when the name of the middleware
// matches the `node.type` (defined in a parser middleware)
.set('bar', function () {})
.set('baz', function () {})
.compile()
See the examples.
Parsers
Parsers are middleware functions used for parsing a string into an ast node.
var ast = snapdragon.parser(str, options)
.use(function() {
var pos = this.position();
var m = this.match(/^\./);
if (!m) return;
return pos({
// `type` specifies the compiler to use
type: 'dot',
val: m[0]
});
})
AST node
When the parser finds a match, pos()
is called, pushing a token for that node onto the ast that looks something like:
{ type: 'dot',
val: '.',
position:
{ start: { lineno: 1, column: 1 },
end: { lineno: 1, column: 2 } }}
Renderers
Renderers are named middleware functions that visit over an array of ast nodes to compile a string.
var res = snapdragon.compiler(ast)
.set('dot', function (node) {
console.log(node.val)
//=> '.'
return this.emit(node.val);
})
Source maps
If you want source map support, make sure to emit the position as well.
var res = snapdragon.compiler(ast)
.set('dot', function (node) {
return this.emit(node.val, node.position);
})
A parser middleware is a function that returns an abject called a token
. This token is pushed onto the AST as a node.
Example token
{ type: 'dot',
val: '.',
position:
{ start: { lineno: 1, column: 1 },
end: { lineno: 1, column: 2 } }}
Example parser middleware
Match a single .
in a string:
this.position()
.match
methodundefined
pos()
is called, which returns a token with:type
: the name of the [compiler] to useval
: The actual value captured by the regex. In this case, a .
. Note that you can capture and return whatever will be needed by the corresponding [compiler].Renderers are run when the name of the compiler middleware matches the type
defined on an ast node
(which is defined in a parser).
Example
Exercise: Parse a dot, then compile it as an escaped dot.
var ast = snapdragon.parser('.')
.use(function () {
var pos = this.position();
var m = this.match(/^\./);
if (!m) return;
return pos({
// define the `type` of compiler to use
type: 'dot',
val: m[0]
})
})
var result = snapdragon.compiler(ast)
.set('dot', function (node) {
return this.emit('\\' + node.val);
})
.compile()
console.log(result.output);
//=> '\.'
Create a new Parser
with the given input
and options
.
Params
input
{String}options
{Object}Define a non-enumberable property on the Parser
instance.
Example
parser.define('foo', 'bar');
Params
key
{String}: propery nameval
{any}: property valuereturns
{Object}: Returns the Parser instance for chaining.Set parser name
with the given fn
Params
name
{String}fn
{Function}Get parser name
Params
name
{String}Push a token
onto the type
stack.
Params
type
{String}returns
{Object} token
Pop a token off of the type
stack
Params
type
{String}returns
{Object}: Returns a tokenReturn true if inside a stack
node. Types are braces
, parens
or brackets
.
Params
type
{String}returns
{Boolean}Example
parser.isType(node, 'brace');
Params
node
{Object}type
{String}returns
{Boolean}Define a non-enumberable property on the Compiler
instance.
Example
compiler.define('foo', 'bar');
Params
key
{String}: propery nameval
{any}: property valuereturns
{Object}: Returns the Compiler instance for chaining.Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Commits | Contributor |
---|---|
106 | jonschlinkert |
2 | doowb |
(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)
To generate the readme and API documentation with verb:
$ npm install -g verb verb-generate-readme && verb
Install dev dependencies:
$ npm install -d && npm test
Jon Schlinkert
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb-generate-readme, v0.1.31, on October 10, 2016.
FAQs
Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map support.
The npm package snapdragon receives a total of 1,920,638 weekly downloads. As such, snapdragon popularity was classified as popular.
We found that snapdragon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.